import type { RowData, TableColumn } from 'naive-ui/es/data-table/src/interface';
import type { ComputedRef } from 'vue';
import { isNumber } from 'lodash-es';

export function useTableWidth(fields: ComputedRef<Array<TableColumn<RowData>>>) {
  return computed(() =>
    fields.value.reduce((width, field) => {
      if (field.type == 'expand' || field.type == 'selection') {
        return 80;
      }

      if (isNumber(field.width)) {
        return width + field.width;
      }
      if (isNumber(field.minWidth)) {
        return width + field.minWidth;
      }
      if (field.title) {
        return width + field.title.length * 14 * 2;
      }

      return width + 120;
    }, 0),
  );
}
